<template>
  <ul class="sub-category">
      <li
      v-for="sub in subs"
      :key="sub.id"
      is="router-link"
      :to="{path:'/list',query:{url:sub.url}}"
      tag="li"
      >
      <img :src="sub.imageUrl" alt="" />
      <span>{{sub.title}}</span>
      </li>
  </ul>
</template>

<script>
import { getSubCateogries } from '@/api/category'
export default {
  name: 'SubCateory',
  data () {
    return {
      subs: []
    }
  },
  // created () {
  //   // console.log(this)
  //   getSubCateogries(this.$route.params.id)
  //   // console.log(this.$route.params.id)
  //     .then(res => {
  //       console.log(res)
  //       this.subs = res
  //     })
  // },
  beforeRouteEnter (to, from, next) {
    console.log('before route enter:', this)
    next((vm) => {
      console.log('before route enter next 回调:', vm)
      getSubCateogries(vm.$route.params.id)
      // console.log(this.$route.params.id)
        .then(res => {
          console.log(res)
          vm.subs = res
        })
    })
  },
  beforeRouteUpdate (to, from, next) {
    console.log('before route update')
    getSubCateogries(to.params.id)
    // console.log(this.$route.params.id)
      .then(res => {
        console.log(res)
        this.subs = res
      })
      //  一定要调用next（） 否则路由导航回在这停止
    next()
  },
  beforeRouteLeave (to, from, next) {
    //  导航离开该组件的对应路由时调用
    //  可以访问组件实例this
    console.log('before route leave')
    next()
  }
}
</script>

<style lang="scss" scoped>
  .sub-category{
    display: flex;
    flex-wrap: wrap;
    width: 80%;
    li{
      width: 45%;
      img{
        width: 100%;
        height: 100%;
      }
    }
    span{
      display: block;
      text-align: center;
    }
  }
</style>
